package pers.jsan.litheopen.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    /**
     * @param plaintext  明文
     * @return 32位密文
     */
    public static String encryption32(String plaintext) {
        String reMd5 = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plaintext.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuilder buf = new StringBuilder();
            for (byte aB : b) {
                i = aB;
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            reMd5 = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reMd5;
    }

    /**
     * 16位MD5加密
     * @param plaintext 明文
     * @return
     */
    public static String encryption16(String plaintext) {
        return encryption32(plaintext).substring(8, 24);
    }
}
